package com.vtquang.mygame2d.screen;

import java.util.List;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.vtquang.mygame2d.actor.Broken;
import com.vtquang.mygame2d.actor.Supermaric;

public class ScreenLevel1 extends BaseScreen {

	/** The map. */
	private TiledMap map;
	/** The renderer. */
	private OrthogonalTiledMapRenderer renderer;
	/** SpriteBatch */
	private SpriteBatch spriteBatch;
	/** The camera. */
	private OrthographicCamera camera;
	/** Supermaric. */
	private Supermaric supermaric;
	/** Broken */
	private List<Broken> brokens;

	public ScreenLevel1(Game game) {
		super(game);
	}

	@Override
	public void show() {
		super.show();

		// load the map, set the unit scale to 1/16 (1 unit == 16 pixels)
		map 	 	 = new TmxMapLoader().load("maps/level1.tmx");
		renderer 	 = new OrthogonalTiledMapRenderer(map, 1 / 16f);
		spriteBatch  = (SpriteBatch) renderer.getSpriteBatch();

		// load supermaric
		supermaric = new Supermaric(new Texture("data/koalio.png"), map);
		supermaric.position = new Vector2(15, 30);

		// setup camera
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 30, 20);
		camera.update();
	}

	@Override
	public void render(float delta) {
		// clear the screen
		Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
 
		// update nhan vat
		supermaric.update(delta);

		// cho camera di chuyen theo nhan vat
		camera.position.x = supermaric.position.x;
		camera.position.x = camera.position.x - 20 > 0 ? camera.position.x : 20;
		camera.update();

		// render map
		renderer.setView(camera);
		renderer.render();

		// draw nhan vat
		spriteBatch.begin();
		supermaric.draw(spriteBatch);
		spriteBatch.end();
	}
	
	@Override
	public TextureAtlas getAtlas() {
		if (atlas == null) {
			atlas = new TextureAtlas(Gdx.files.internal("datapacker/atlas.txt"));
		}
		return atlas;
	}
}
